package com.nifostasky.comic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nifostasky.comic.entitys.SysRoleMenu;
import com.nifostasky.comic.exceptions.BusinessException;
import com.nifostasky.comic.mapper.SysRoleMenuMapper;
import com.nifostasky.comic.service.SysRoleMenuService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements SysRoleMenuService {

    @Override
    public Boolean deleteRoleMenuRelByRoleId(Long roleId) {
        if(Objects.isNull(roleId)){
            throw new BusinessException("缺少必填参数");
        }
        LambdaQueryWrapper<SysRoleMenu> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(SysRoleMenu::getRoleId,roleId);
        return this.remove(queryWrapper);
    }

    @Override
    public List<Long> getRoleMenuByRoleId(Long roleId) {
        LambdaQueryWrapper<SysRoleMenu> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(SysRoleMenu::getRoleId,roleId);
        List<SysRoleMenu> listRoleMenu = this.list(queryWrapper);
        if(CollectionUtils.isEmpty(listRoleMenu)){
            return null;
        }
        List<Long> listMenuId = listRoleMenu.stream().map(sysRoleMenu -> sysRoleMenu.getMenuId()).collect(Collectors.toList());
        return listMenuId;
    }
}
